#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
    char data[MAX_SIZE];
    int top;
} Stack;
void init(Stack *s) {
    s->top = -1;
}
bool isEmpty(Stack *s) {
    return s->top == -1;
}
void push(Stack *s, char c) {
    if (s->top == MAX_SIZE - 1) return;
    s->data[++s->top] = c;
}
char pop(Stack *s) {
    if (isEmpty(s)) return '\0';
    return s->data[s->top--];
}
char peek(Stack *s) {
    if (isEmpty(s)) return '\0';
    return s->data[s->top];
}
bool isMatch(char left, char right) {
    return (left == '(' && right == ')') ||
           (left == '[' && right == ']') ||
           (left == '{' && right == '}');
}
bool isValid(char *s) {
    Stack stack;
    init(&stack);
    for (int i = 0; s[i] != '\0'; i++) {
        if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
            push(&stack, s[i]);
        } else if (s[i] == ')' || s[i] == ']' || s[i] == '}') {
            if (isEmpty(&stack) || !isMatch(peek(&stack), s[i])) {
                return false;
            }
            pop(&stack);
        }
    }
    return isEmpty(&stack);
}
int main() {
    char input[MAX_SIZE];
    printf("请输入括号序列: ");
    scanf("%s", input);
    if (isValid(input)) {
        printf("括号匹配正确\n");
    } else {
        printf("括号匹配错误\n");
    }
    return 0;
}
